import os
import sys

def paramloc(f):
    return "/home/eduardo/os/self-healing-disk-sim/disksim/valid/" + f



def modify_sh_config_np(fname,din,dout):
    fh = open('templateNP.config')
    wh = open(fname,'w')

    lines = fh.readlines()
    transmap = (('[DP]',paramloc(din)),('[DO]',dout))
    for l in lines:
        for (a,ta) in transmap:
            if l.find(a) >= 0:
                l = l.replace(a,ta)
        wh.write(l)
    fh.close()
    wh.close()

def modify_sh_config(fname,pgs,mdi,pf,rm,msz,mtm,din,dout):
    fh = open('template.config')
    wh = open(fname,'w')

    lines = fh.readlines()
    transmap = (('[PGS]',pgs),('[MDI]',mdi),('[PF]',pf),('[RM]',rm),('[MSZ]',msz),('[MTM]',mtm),('[DP]',paramloc(din)),('[DO]',dout))
    for l in lines:
        for (a,ta) in transmap:
            if l.find(a) >= 0:
                l = l.replace(a,ta)
        wh.write(l)
    fh.close()
    wh.close()







conf = ['no_parity','fragmented']
sched = ['self3.parv']
trace = ['trace1','trace2','trace3']
par_grup_sz = ['8','16','32']
mod_sz  = ['8','16','32']
time_mem = ['100','500']

i = 0
for s in sched:
    for t in trace:
        aux =  "_".join([str(0),str(0),'no_parity','false',str(1024),str(0),s,t])
        fname = "cfg/conf_" + aux + ".cfg"
        dname = "ds/dkout_" + aux  + ".cfg"
        oname = "out/out_" + aux  + ".cfg"
        modify_sh_config_np(fname,s,dname)
        os.system("java -jar sh.jar " + fname  + " traces/" + t + ".blkparse " + oname)

rm = 'true'
for s in sched:
    for t in trace: 
        for pgs in par_grup_sz:
            for mdi in mod_sz:
                for mt in time_mem:
                    aux = "_".join([pgs,mdi,'fragmented',rm,'1024',mt,s,t])
                    fname = "cfg/conf_" + aux  + ".cfg"
                    dname = "ds/dkout_" + aux  + ".cfg"
                    oname = "out/out_" + aux  + ".cfg"
                    modify_sh_config(fname,pgs,mdi,'fragmented',rm,'1024',mt,s,dname)
                    os.system("java -jar sh.jar " + fname  + " traces/" + t + ".blkparse " + oname)

        
        
rm = 'false'
for s in sched:
    for t in trace: 
        for pgs in par_grup_sz:
            for mdi in mod_sz:
                aux = "_".join([pgs,mdi,'fragmented',rm,'1024',str(0),s,t])
                fname = "cfg/conf_" + aux  + ".cfg"
                dname = "ds/dkout_" + aux  + ".cfg"
                oname = "out/out_" + aux  + ".cfg"
                modify_sh_config(fname,pgs,mdi,'fragmented',rm,'1024',str(0),s,dname)
                os.system("java -jar sh.jar " + fname  + " traces/" + t + ".blkparse " + oname)
                  










